home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_218 / mandel / src / display.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  308 lines

  1. /*
  2.  * M A N D E L B R O T    C O N S T R U C T I O N   S E T
  3.  *
  4.  * (C) Copyright 1989 by Olaf Seibert.
  5.  * Mandel may be freely distributed. See file 'doc/Notice' for details.
  6.  *
  7.  * The Display routines; initialisation and de-initialisation of
  8.  * screen and window.
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <intuition/intuition.h>
  13. #ifdef DEBUG
  14. #   include <stdio.h>
  15. #   undef STATIC
  16. #   define STATIC   /* EMPTY */
  17. #endif
  18. #include "mandel.h"
  19.  
  20. #define BUGHEIGHT 213    /* See later comment */
  21. #define BUGWIDTH  640
  22.  
  23. USHORT ColorMap[MAXDEPTH];
  24. bool ColorMapValid = FALSE;
  25.  
  26.  
  27. /*
  28.  *  Initialize the screen and the windows and the menus
  29.  *  Return value indicates FAILURE.
  30.  */
  31.  
  32. bool InitDisplay(borderless)
  33. bool borderless;
  34. {
  35.     USHORT ViewModes = MandelNScreen.ViewModes;
  36.     struct Screen WBScreen;
  37.     int i;
  38.  
  39.     StopFraming();
  40.  
  41.     Saved = TRUE;   /* We can easily reconstruct this `picture' */
  42.  
  43.     if (WBWidth == 0) { /* Get Left, Top, Width, Height */
  44.     GetScreenData(&WBScreen, (long)sizeof(WBScreen),
  45.         (long)WBENCHSCREEN, NULL);
  46.     WBWidth = WBScreen.Width;
  47.     WBHeight = WBScreen.Height;
  48.     /* Maybe we have an interlaced WorkBench screen */
  49.     if (WBScreen.ViewPort.Modes & LACE) WBHeight >>= 1;
  50.     /* And maybe, if we use MWB, it isn't hires... */
  51.     if (!(WBScreen.ViewPort.Modes & HIRES)) WBWidth <<= 1;
  52.     }
  53.     MandelNScreen.Width = WBWidth;
  54.     MandelNScreen.Height = WBHeight;
  55.  
  56.     if (ViewModes & LACE) MandelNScreen.Height <<= 1;
  57.     if (MandelNScreen.Height < MINSCREENHEIGHT)
  58.     MandelNScreen.Height = MINSCREENHEIGHT;
  59.  
  60.     if (ViewModes & HIRES) {
  61.     MandelNScreen.Depth = 4;
  62.  
  63.     /* Avoid a bug in MrgCop()?? if you have a PAL machine with */
  64.     /* a screen wider than 640 taller than 213 and with 4 bitplanes */
  65.  
  66.     if (MandelNScreen.Width > BUGWIDTH && MandelNScreen.Height > BUGHEIGHT)
  67.         MandelNScreen.Width = BUGWIDTH;
  68.     } else {
  69.     MandelNScreen.Width >>= 1;
  70.     if (ViewModes & EXTRA_HALFBRITE) {
  71.         MandelNScreen.Depth = 6;
  72.     } else {
  73.         MandelNScreen.Depth = 5;
  74.     }
  75.     }
  76.  
  77.     if (MandelNScreen.Width < MINSCREENWIDTH)
  78.     MandelNScreen.Width = MINSCREENWIDTH;
  79.  
  80.     NumColors = 1 << MandelNScreen.Depth;
  81.  
  82.     if ( !MandelScreen && !(MandelScreen = OpenScreen(&MandelNScreen)) ) {
  83.     skipto abort;
  84.     }
  85.  
  86.     if (ColorMapValid)
  87.     LoadRGB4(&MandelScreen->ViewPort, ColorMap, (long) NumColors);
  88.  
  89.     MainNWindow.Screen = MandelScreen;
  90.  
  91.     if (MainNWindow.Height < MainNWindow.MinHeight) {
  92.     /* First time we open the window */
  93.     MainNWindow.LeftEdge = 0;
  94.     MainNWindow.TopEdge  = MandelScreen->BarHeight;
  95.     MainNWindow.Width    = MandelScreen->Width;
  96.     MainNWindow.Height   = MandelScreen->Height - MainNWindow.TopEdge;
  97.     } else {
  98.     if (ViewModes & HIRES) {
  99.         MainNWindow.LeftEdge <<= 1;
  100.         MainNWindow.Width <<= 1;
  101.     }
  102.     if (ViewModes & LACE) {
  103.         MainNWindow.TopEdge <<= 1;
  104.         MainNWindow.Height <<= 1;
  105.     }
  106.     }
  107.  
  108.     /* Check for windows that can't be opened */
  109.  
  110.     if (MainNWindow.LeftEdge + MainNWindow.Width > MandelScreen->Width)
  111.     MainNWindow.Width = MandelScreen->Width - MainNWindow.LeftEdge;
  112.  
  113.     if (MainNWindow.TopEdge + MainNWindow.Height > MandelScreen->Height)
  114.     MainNWindow.Height = MandelScreen->Height - MainNWindow.TopEdge;
  115.  
  116.     if ( !MainWindow && !(MainWindow = OpenWindow(&MainNWindow)) ) {
  117.     skipto abort;
  118.     }
  119.  
  120.     SetMenuStrip(MainWindow, MandelMenu);
  121.     if (borderless) {
  122.     DoBorderless(MainWindow, &borderinfo);
  123.     }
  124.  
  125.     InitPenTable();
  126.  
  127.     return FALSE;
  128.  
  129. abort:
  130.     CleanupDisplay((bool) TRUE);
  131.     return TRUE;
  132.  
  133. }
  134.  
  135. bool CleanupDisplay(everything)
  136. bool everything;
  137. {
  138.     bool wasborderless;
  139.     int i;
  140.  
  141.     StopDrawing();
  142.     StopFraming();
  143.  
  144.     CloseColorWindow((BOOL) FALSE);
  145.  
  146.     wasborderless = (MainWindow != NULL) &&
  147.     ((MainWindow->Flags & BORDERLESS) != 0);
  148.  
  149.     if (MainWindow) {
  150.     /* Save window appearance for later, in low-res non-lace pixels */
  151.     MainNWindow.LeftEdge = MainWindow->LeftEdge;
  152.     MainNWindow.TopEdge  = MainWindow->TopEdge;
  153.     MainNWindow.Width    = MainWindow->Width;
  154.     MainNWindow.Height   = MainWindow->Height;
  155.  
  156.     ClearMenuStrip(MainWindow);     /* Remove menu strip */
  157.     CloseWindow(MainWindow);        /* Finally close it */
  158.     MainWindow = NULL;
  159.     }
  160.  
  161.     /* Save the colors we may have established with great care */
  162.  
  163.     if (MandelScreen) {
  164.     for (i=0; i < NumColors; i++) {
  165.         ColorMap[i] = GetRGB4(MandelScreen->ViewPort.ColorMap, i);
  166.     }
  167.     ColorMapValid = TRUE;
  168.  
  169.     if (MandelScreen->ViewPort.Modes & HIRES) {
  170.         MainNWindow.LeftEdge >>= 1;
  171.         MainNWindow.Width >>= 1;
  172.     }
  173.     if (MandelScreen->ViewPort.Modes & LACE) {
  174.         MainNWindow.TopEdge >>= 1;
  175.         MainNWindow.Height >>= 1;
  176.     }
  177.  
  178.     /* Close the screen only if `everything' must be cleaned up */
  179.  
  180.     if (everything) {
  181.         CloseScreen(MandelScreen);
  182.         MandelScreen = NULL;
  183.     }
  184.     }
  185.  
  186.     return wasborderless;
  187. }
  188.  
  189. bool ReInitDisplay()
  190. {
  191.     bool WasBorderless;
  192.  
  193.     WasBorderless = CleanupDisplay((bool) TRUE);
  194.     if (InitDisplay(WasBorderless)) { /* Trouble */
  195.     MandelNScreen.ViewModes &= ~(HIRES | LACE | EXTRA_HALFBRITE);
  196.     WBWidth = 0;    /* Re-use Workbench screen size */
  197.     if (InitDisplay((bool) FALSE))
  198.         MyExit("Can't re-init display - Maybe low on memory");
  199.     SelectMenu(MENU(OPTMENU, OPTRES, ORHI), (bool)FALSE);
  200.     SelectMenu(MENU(OPTMENU, OPTRES, ORILC), (bool)FALSE);
  201.     SelectMenu(MENU(OPTMENU, OPTRES, ORBCK), (bool)FALSE);
  202.     SelectMenu(MENU(OPTMENU, OPTRES, OREHB), (bool)FALSE);
  203.  
  204.     return TRUE;
  205.     }
  206.  
  207.     return FALSE;
  208. }
  209.  
  210. bool DoBorderless(window, borderinfo)
  211. struct Window *window;
  212. struct BorderInfo *borderinfo;
  213. {
  214.     /* Make window borderless */
  215.  
  216. /* register struct Layer_Info *LayerInfo = &window->WScreen->LayerInfo; */
  217. #define LayerInfo   NULL        /* Since V1.2 */
  218.     register struct Layer *Layer = window->RPort->Layer;
  219.     register long movex;
  220.     register long movey;
  221.     register long sizex;
  222.     register long sizey;
  223.     bool Success = FALSE;
  224.  
  225.     if (window->Flags & BORDERLESS) return TRUE;
  226.  
  227.     LockLayer(LayerInfo, Layer);
  228.  
  229.     window->Flags |= BORDERLESS;
  230.  
  231.     movex = -window->BorderLeft;
  232.     movey = -window->BorderTop;
  233.     sizex = window->BorderRight - movex;
  234.     sizey = window->BorderBottom - movey;
  235.  
  236.     window->BorderLeft =
  237.     window->BorderTop =
  238.     window->BorderRight =
  239.     window->BorderBottom = 0;
  240.  
  241.     window->GZZWidth += sizex;
  242.     window->GZZHeight += sizey;
  243.  
  244.     if ( MoveLayer(LayerInfo, Layer, movex, movey) ) {
  245.     Success = SizeLayer(LayerInfo, Layer, sizex, sizey);
  246.     if (!Success) {
  247.         sizex = sizey = 0;
  248.     }
  249.     } else {
  250.     movex = movey = sizex = sizey = 0;
  251.     }
  252.     UnlockLayer(Layer);
  253.  
  254.     borderinfo->MoveX = movex;
  255.     borderinfo->MoveY = movey;
  256.     borderinfo->SizeX = sizex;
  257.     borderinfo->SizeY = sizey;
  258.  
  259.     /* Done making borderless */
  260.  
  261.     if (Success) {
  262.     return Success;
  263.     } else {    /* Try to clean up the mess */
  264.     UndoBorderless(window, borderinfo);
  265.     return FALSE;
  266.     }
  267. #undef LayerInfo
  268. }
  269.  
  270. void UndoBorderless(window, borderinfo)
  271. struct Window *window;
  272. struct BorderInfo *borderinfo;
  273. {
  274.     /* ``Another fine mess you got me into!'' (Oliver Hardy) */
  275.  
  276. /*  register struct Layer_Info *LayerInfo = &window->WScreen->LayerInfo; */
  277. #define LayerInfo   NULL        /* Since V1.2 */
  278.     register struct Layer *Layer = window->RPort->Layer;
  279.     register long movex = borderinfo->MoveX;
  280.     register long movey = borderinfo->MoveY;
  281.     register long sizex = borderinfo->SizeX;
  282.     register long sizey = borderinfo->SizeY;
  283.     bool Success = FALSE;
  284.  
  285.     if (!(window->Flags & BORDERLESS))  return;
  286.  
  287.     LockLayer(LayerInfo, Layer);
  288.     SizeLayer(LayerInfo, Layer, -sizex, -sizey);
  289.     MoveLayer(LayerInfo, Layer, -movex, -movey);
  290.  
  291.     window->GZZWidth -= sizex;
  292.     window->GZZHeight -= sizey;
  293.  
  294.     window->BorderLeft = -movex;
  295.     window->BorderTop = -movey;
  296.     window->BorderRight = sizex + movex;
  297.     window->BorderBottom = sizey + movey;
  298.  
  299.     window->Flags &= ~BORDERLESS;
  300.  
  301.     RefreshWindowFrame(window);
  302.  
  303.     UnlockLayer(Layer);
  304.  
  305.     /* Done Undoing Borderless */
  306. #undef LayerInfo
  307. }
  308.